from pages.login_page import LoginPage
from pages.select_page import SelectPage
from selenium import webdriver
import time
from selenium.webdriver.common.by import By


class DbPage(SelectPage, LoginPage):
    create_db_button_loc = (
        By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/div[1]/ul[1]/li[1]/button[1]/span[1]")
    database_name_loc = (By.XPATH,
                         "/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/form[1]/div[1]/"
                         "div[1]/div[1]/div[1]/div[1]/span[1]/div[1]/input[1]")
    database_type_loc = (By.XPATH, "//input[@placeholder='请选择']")
    select_loc = (By.XPATH, "/html[1]/body[1]/div[2]/div[1]/div[1]")
    sqlserver_loc = (By.XPATH, "/html[1]/body[1]/div[2]/div[1]/div[1]/ul[1]/li[1]/span[1]")
    mysql_loc = (By.XPATH, "/html[1]/body[1]/div[2]/div[1]/div[1]/ul[1]/li[2]/span[1]")
    db2_loc = (By.XPATH, "/html[1]/body[1]/div[2]/div[1]/div[1]/ul[1]/li[3]/span[1]")
    orcale_loc = (By.XPATH, "/html[1]/body[1]/div[2]/div[1]/div[1]/ul[1]/li[4]/span[1]")
    ip_loc = (By.XPATH, "//div[@class='elFormBottom el-row']//div[2]//div[2]//div[1]//div[1]"
                        "//span[1]//div[1]//input[1]")
    port_loc = (By.XPATH, "//div[3]//div[1]//div[1]//div[1]//span[1]//div[1]//input[1]")
    db_loc = (By.XPATH, "//div[3]//div[2]//div[1]//div[1]//span[1]//div[1]//input[1]")
    db_user_loc = (By.XPATH, "//div[4]//div[1]//div[1]//div[1]//span[1]//div[1]//input[1]")
    db_pwd_loc = (By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/form[1]/div[1]/div[4]"
                            "/div[2]/div[1]/div[1]/div[1]/input[1]")
    connect_test_loc = (By.XPATH, "//button[@class='el-button el-button--default el-button--mini add_btn']")
    message_text_loc = (By.CLASS_NAME, "box_message")
    ok_button_loc = (By.XPATH, "/html[1]/body[1]/div[3]/div[1]/div[3]/button[1]")
    submit_button_loc = (By.XPATH,
                         "/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/form[1]/div[2]/div[1]/div["
                         "1]/div[1]/div[1]/button[1]/span[1]")
    message_text_loc1 = (By.CLASS_NAME, "box_message")

    def db_manage_page(self):
        user = 'admin'
        pwd = '111111-a'
        self.login(user, pwd)
        self.click_scheduler_menu()
        # 点击进入数据库管理页面
        self.click_database_menu()

    def click_create_button(self):
        """ 点击数据库创建按钮，进入数据库创建页面 """
        self.db_manage_page()
        # 切到父级frame
        self.driver.switch_to.parent_frame()
        self.driver.switch_to.parent_frame()
        # 进入创建数据库iframe
        self.driver.switch_to.frame("_blank")
        self.click(self.create_db_button_loc)

    def create_db(self, db_name, ip, port, db, db_user, db_pwd, db_type=''):
        self.sendkeys(self.database_name_loc, db_name)
        self.click(self.database_type_loc)
        time.sleep(0.5)
        # 根据输入数据库类型选择
        if db_type == 'mysql':
            self.click(self.mysql_loc)
        elif db_type == 'db2':
            self.click(self.db2_loc)
        elif db_type == 'sqlserver':
            self.click(self.sqlserver_loc)
        else:
            self.click(self.orcale_loc)
        self.sendkeys(self.ip_loc, ip)
        self.sendkeys(self.port_loc, port)
        self.sendkeys(self.db_loc, db)
        self.sendkeys(self.db_user_loc, db_user)
        self.sendkeys(self.db_pwd_loc, db_pwd)
        self.click(self.connect_test_loc)
        # 获取提示信息文本,测试链接按钮功能验证
        time.sleep(0.5)
        text = self.gettext(self.message_text_loc)
        if text == "数据库连接成功！":
            pass
        else:
            print("测试链接失败")
        # 点击确定按钮
        self.click(self.ok_button_loc)
        # 点击提交按钮
        self.click(self.submit_button_loc)

    def get_submit_message(self):
        time.sleep(0.5)
        text1 = self.gettext(self.message_text_loc1)
        # if text1 == "处理成功":
        #     pass
        # else:
        #     print("提交失败")
        # 点击确定按钮
        return text1

    def click_ok_button(self, loc):
        # 点击确定按钮
        self.click(self.ok_button_loc)


if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get('http://172.28.30.2:9000/platform/#/login')
    driver.implicitly_wait(10)  # 设置浏览器隐式等待时间
    driver.maximize_window()
    d = DbPage(driver)
    # d.click_create_button()
    # db_name = "oracle2"
    # ip = "172.28.28.168"
    # port = '1521'
    # db = "orcl"
    # db_user = "dhcc27"
    # db_pwd = "dhcc270716"
    # d.create_db(db_name, ip, port, db, db_user, db_pwd)
    # driver.quit()
    d.db_manage_page()
